package com.longg.dao;

import com.longg.comm.Comm;
import com.longg.dao.base.BaseDao;
import com.longg.model.UserFocus;
import com.longg.utils.JDBCUtil;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import sun.management.jdp.JdpController;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class UserFocusDao extends BaseDao<UserFocus> {
    public UserFocusDao() {
        super(UserFocus.class);
    }


    public List<Map<String, Object>> findAllById(int id,int page) {
        String sql = "select ui.real_name,uf.user_focus_id \n" +
                "from user_info ui join user_focus uf\n" +
                "where uf.user_id = ? and ui.user_id = uf.user_focus_id" +
                " limit ?,?";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        List<Map<String, Object>> query = null;
        try {
            query = queryRunner.query(sql, new MapListHandler(), id,(page - 1) * Comm.PAGE_SIZE,Comm.PAGE_SIZE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    public long getFocusCount(int id){
        String sql = "select count(*) from user_focus where user_id = ?";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        long count = 0;
        try {
            count = queryRunner.query(sql,new ScalarHandler<Long>(),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }


}
